package com.gangan.controller.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gangan.controller.common.BaseController;
import com.gangan.model.CmsContent;
import com.gangan.model.Order;
import com.gangan.model.Patient;
import com.gangan.model.Section;
import com.gangan.service.ICmsContentService;
import com.gangan.service.IDoctorService;
import com.gangan.service.IOrderService;
import com.gangan.tools.MessageBean;
import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.ehcache.search.expression.Or;
import org.apache.ibatis.binding.MapperMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


import java.util.List;
import java.util.Map;

@Api(description = "*预约模块模块控制层")
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Autowired
    IOrderService orderService;

    @Autowired(required = true)
    IDoctorService doctorService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "订单数据列表", notes = "根据相关条件查询列表数据")
    public MessageBean list(Page page) {
        return resultSuccess(orderService.page(page, Wrappers.<Order>query().lambda().orderByDesc(Order::getUpdateTime)));
    }

    @RequestMapping(value = "/getOrderByOthers", method = RequestMethod.POST)
    @ApiOperation(value = "根据状态或者id或者预约人来查询订单", notes = "根据状态或者id或者预约人来查询订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "patientName", dataType = "String", required = false, value = "预约人", defaultValue = "老王"),
            @ApiImplicitParam(paramType = "query", name = "status", dataType = "Integer", required = false, value = "订单状态, 0-未确认,1-预约成功,2-待诊,3-已完成,4-取消/预约失败", defaultValue = "0"),
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "Integer", required = false, value = "预约人Id", defaultValue = "1")})
    public MessageBean getOrderByOthers(String patientName, Integer status, Integer id) {
        List<Map> orderByOthers = orderService.getOrderByOthers(patientName, status, id);
        return resultSuccess(orderService.getOrderByOthers(patientName, status, id));
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除订单", notes = "根据传入的订单id数组删除订单")
    public MessageBean del(@RequestParam(value = "idList") List<Integer> idList) {
        return resultSuccess(orderService.removeByIds(idList));
    }

    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @ApiOperation(value = "移除数据", notes = "根据自增主键移除数据")
    public MessageBean del(Integer id) {
        return resultSuccess(orderService.removeById(id));
    }

    @RequestMapping(value = "/queryDoctor", method = RequestMethod.POST)
    @ApiOperation(value = "预约挂号", notes = "查询科室下所有的医生")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "department", dataType = "String", required = true, value = "所属科室", defaultValue = "肛肠科")})
    public MessageBean getDoctorBydepartment(String department) {
        return resultSuccess(orderService.querydocBydep(department));
    }

    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ApiOperation(value = "医生主页", notes = "根据医生Id查询医生信息")
    public MessageBean finDocById(Integer id) {
        return resultSuccess(doctorService.getById(id));
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "预约挂号", notes = "预约填写")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "Integer", required = true, value = "订单列表Id")})
    public MessageBean add(Order add) {
        return resultSuccess(orderService.save(add));
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "编辑数据", notes = "根据主键修改数据")
    public MessageBean edit(Order edit) {
        return resultSuccess(orderService.updateById(edit));
    }

}
